<?php

namespace app\model;

use app\service\System;
use app\stateData;
use think\Model;

class SystemFieldPower extends Model
{
    protected $createTime = 'create_date';


    /**
     * 添加字段权限
     * @param $data
     * @return \think\Collection
     * @throws \Exception
     */
    public function addFieldPowerModel($data): \think\Collection
    {
        return $this->saveAll($data);
    }

    /**
     *查询字段id绑定的可见权限
     * @param $fieldId
     * @param $power_type
     * @return array|SystemFieldPower[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFieldPowerModel($fieldId,$power_type): array
    {
        return $this->where(['field_id'=>$fieldId,'power_type'=>$power_type])->select()->toArray();
    }

    /**
     * 删除字段|菜单权限
     * @param $field
     * @return bool
     */
    public function delFieldPowerModel($field): bool
    {
        return $this->where(['field_id'=>$field])->delete();
    }

    /**
     * 条件删除
     * @param $field
     * @param $accType
     * @param $powerType
     * @param $fid
     * @return bool
     */
    public function delWhereFieldPowerModel($field,$accType,$powerType,$fid): bool
    {
        return $this->where(['field_id'=>$field,'account_type'=>$accType,'power_type'=>$powerType,'from_id'=>$fid])->delete();
    }

    /**
     * 获取用户菜单权限
     * @param $id
     * @param $account_type
     * @return array
     */
    public function getUserMenuPower($id,$account_type): array
    {
        return $this->where([
            'roles_id'=>$id,
            'account_type'=>$account_type,
            'power_type'=>stateData::$_power_type['menu'],
        ])->column('field_id');
    }

    /**
     * 删除菜单权限
     * @param $account_type
     * @param $roles_id
     * @return bool
     */
    public function delMenuPowerModel($account_type, $roles_id): bool
    {
        return $this->where([
            'roles_id'=>$roles_id,
            'account_type'=>$account_type,
            'power_type'=>stateData::$_power_type['menu'],
        ])->delete();
    }

    /**
     * 获取用户字段权限
     * @param $id
     * @param $account_type
     * @return array
     */
    public function getUserFieldPower($id,$account_type): array
    {

        return $this->where([
            'roles_id'=>$id,
            'account_type'=>$account_type,
            'power_type'=>stateData::$_power_type['field'],
        ])->column('field_id');
    }

}